/****************************************************************************
**
** Copyright (C) 2010 Tieto Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Tieto Corporation (info.ostrava@tieto.com)
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Tieto.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Tieto gives you certain additional
** rights.  These rights are described in the Tieto Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Tieto at info.ostrava@tieto.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef AREAINDEXINGMODEL_H
#define AREAINDEXINGMODEL_H

#include "imagemodel.h"
#include "backgroundtask.h"

class AreaIndexingModel;

/**
 * Background task for indexing areas
 */
class AreaIndexingTask : public BackgroundTask
{

    Q_OBJECT

public:

    /**
     * Constructor
     * @param model model
     */
    AreaIndexingTask(AreaIndexingModel &model, QObject *parent = 0);

protected:

    /**
     * Background task method
     */
    virtual void doRun();

    /**
     * Background task cancel method
     */
    virtual void doCancel();

private:

    // TODO stack friendly
    /**
     * Scanline fill algorith
     * @param image binary image to be indexed
     * @param indexImage indexed ares represented as image
     * @param row
     * @param col
     * @param oldColor old color
     * @param newColor new color
     * @return true, if area was indexed
     */
    bool fillScanline(ColorImage &image, IndexImage &indexImage, const int row, const int col,
                      const QRgb oldColor, const QRgb newColor);

    /**
     * Areas count
     */
    int _areasCount;

    /**
     * Area color
     */
    QRgb _areaColor;

    /**
     * Color palette
     */
    QVector<QRgb> _pallete;

    /**
     * Model
     */
    AreaIndexingModel& _model;
};


/**
 * Indexing of detected areas
 */

class AreaIndexingModel : public ImageModel
{

Q_OBJECT

public:

    /**
     * Image IDs
     */
    enum ImageID {
        EDetectedAreasImage = 0,    // Detected areas image
        EIndexedAreas,              // Indexed areas
        EIndexedAreasImage          // Indexed areas image
    };

    /**
     * Constructor
     */
    explicit AreaIndexingModel(QObject *parent = 0);

    /**
     * Areas indexing
     * @return area indexing task
     */
    AreaIndexingTask* indexAreas();

    /**
     * Detected areas image
     * @return detected areas image
     */
    BinaryImage detectedAreasImage();

    /**
     * Sets indexed areas image
     * @param indexed areas image
     */
    void setIndexedAreasImage(const ColorImage &image);

    /**
     * Indexed areas image representation
     * @return indexed areas image
     */
    ColorImage indexedAreasImage();

    /**
     * Set indexed areas
     * @param image indexed areas
     */
    void setIndexedAreas(const IndexImage &image);

    /**
     * Indexed areas
     * @return indexed areas
     */
    IndexImage indexedAreas();

signals:

    /**
     * Emits when areas are indexed
     * @param indexedAreas indexed areas
     */
    void areasIndexed(const IndexImage &indexedAreas);


    /**
     * Emits when areas are indexed
     * @param indexedAreasImage indexed areas image
     */
    void areasImageIndexed(const ColorImage &indexedAreasImage);

public slots:

    /**
     * Sets binary image with detected areas for indexing
     * @param image binary image with detected areas
     */
    void setDetectedAreasImage(const BinaryImage& image);

};

#endif // AREAINDEXINGMODEL_H
